package com.amar.dtadmin.apidoc.model;

import lombok.Data;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author xiaoshiqiang
 * @since 2025/2/9
 */
@Data
public class ApiDocGroupResp {
    private String groupid;

    private String name;

    private Integer sortno;

    private Integer level;

    private String grouplevel;

    private String parentgroupid;

    private List<ApiDocGroupResp> nextgroup;

    private String fullName;

    public Map<String, ApiDocGroupResp> flat() {
        Map<String, ApiDocGroupResp> groupMap = new HashMap<>();
        flat(this, groupMap, "");
        return groupMap;
    }

    private void flat(ApiDocGroupResp node, Map<String, ApiDocGroupResp> groupMap, String parentFullName) {
        if (node == null) {
            return;
        }
        // 设置当前节点的全路径
        String currentFullName = parentFullName.isEmpty() ? node.getName() : parentFullName + "-" + node.getName();
        node.setFullName(currentFullName);
        groupMap.put(currentFullName, node);

        // 递归处理所有子节点
        if (node.getNextgroup() != null) {
            for (ApiDocGroupResp child : node.getNextgroup()) {
                flat(child, groupMap, currentFullName);
            }
        }
    }
}
